﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using HealthManagement.Common;
using PersonalHealth.Data.Model;
using HealthManagement.Models;
using UI.Comm.Controls;

namespace HealthManagement.Views
{
    /// <summary>
    /// SystemPanel.xaml 的交互逻辑
    /// </summary>
    public partial class SystemPanel : Page
    {
        Dictionary<string, OfficalDataThreshold> dictData;
        Dictionary<string, MeasureSubType> dictType;
        public SystemPanel()
        {
            InitializeComponent();
            dictData = new Dictionary<string, OfficalDataThreshold>();
            dictType = new Dictionary<string, MeasureSubType>();
            init();
        }

        void init()
        {
            getData();
            IMeasureSubTypeService typeSer = new MeasureSubTypeService();
            var typeList = typeSer.FindAll();
            foreach (var i in typeList)
            {
                dictType.Add(i.Code, i);
            }
        }

        void getData()
        {
            IOfficalDataThresholdService service = new OfficalDataThresholdService();

            dictData = service.GetAllDict();

            ThresholdModel obj = new ThresholdModel();

            if (dictData.ContainsKey("SBP"))
            {
                obj.SBP_Max = dictData["SBP"].Max;
                obj.SBP_Min = dictData["SBP"].Min;
            }

            if (dictData.ContainsKey("DBP"))
            {
                obj.DBP_Max = dictData["DBP"].Max;
                obj.DBP_Min = dictData["DBP"].Min;
            }

            if (dictData.ContainsKey("HR_Value"))
            {
                obj.HR_Value_Max = dictData["HR_Value"].Max;
                obj.HR_Value_Min = dictData["HR_Value"].Min;
            }

            if (dictData.ContainsKey("BloodGlucose"))
            {
                obj.BloodGlucose_Max = dictData["BloodGlucose"].Max;
                obj.BloodGlucose_Min = dictData["BloodGlucose"].Min;
            }

            if (dictData.ContainsKey("SpO2"))
            {
                obj.SpO2_Max = dictData["SpO2"].Max;
                obj.SpO2_Min = dictData["SpO2"].Min;
            }

            if (dictData.ContainsKey("Pulse"))
            {
                obj.Pulse_Max = dictData["Pulse"].Max;
                obj.Pulse_Min = dictData["Pulse"].Min;
            }

            if (dictData.ContainsKey("Temp"))
            {
                obj.Temp_Max = dictData["Temp"].Max;
                obj.Temp_Min = dictData["Temp"].Min;
            }


            if (dictData.ContainsKey("ECG"))
            {
                obj.ECG_Max = dictData["ECG"].Max;
                obj.ECG_Min = dictData["ECG"].Min;
            }

            thresholdData.DataContext = obj;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            List<OfficalDataThreshold> list = new List<OfficalDataThreshold>();
            DateTime now = DateTime.Now;
            ThresholdModel obj = thresholdData.DataContext as ThresholdModel;
            OfficalDataThreshold sbp;
            if (dictData.ContainsKey("SBP"))
            {
                sbp = dictData["SBP"];
            }
            else
            {
                sbp = new OfficalDataThreshold();
                sbp.CreateDate = now;
                sbp.MeasureSubTypeId = dictType["SBP"].Id;
            }
            sbp.Max = obj.SBP_Max;
            sbp.Min = obj.SBP_Min;
            sbp.ModifyDate = now;
            list.Add(sbp);

            OfficalDataThreshold dbp;
            if (dictData.ContainsKey("DBP"))
            {
                dbp = dictData["DBP"];
            }
            else
            {
                dbp = new OfficalDataThreshold();
                dbp.CreateDate = now;
                dbp.MeasureSubTypeId = dictType["DBP"].Id;
            }
            dbp.Max = obj.DBP_Max;
            dbp.Min = obj.DBP_Min;
            dbp.ModifyDate = now;
            list.Add(dbp);

            OfficalDataThreshold hr_Value;
            if (dictData.ContainsKey("HR_Value"))
            {
                hr_Value = dictData["HR_Value"];
            }
            else
            {
                hr_Value = new OfficalDataThreshold();
                hr_Value.CreateDate = now;
                hr_Value.MeasureSubTypeId = dictType["HR_Value"].Id;
            }
            hr_Value.Max = obj.HR_Value_Max;
            hr_Value.Min = obj.HR_Value_Min;
            hr_Value.ModifyDate = now;
            list.Add(hr_Value);

            OfficalDataThreshold bp;
            if (dictData.ContainsKey("BloodGlucose"))
            {
                bp = dictData["BloodGlucose"];
            }
            else
            {
                bp = new OfficalDataThreshold();
                bp.CreateDate = now;
                bp.MeasureSubTypeId = dictType["BloodGlucose"].Id;
            }
            bp.Max = obj.BloodGlucose_Max;
            bp.Min = obj.BloodGlucose_Min;
            bp.ModifyDate = now;
            list.Add(bp);

            OfficalDataThreshold spO2;
            if (dictData.ContainsKey("SpO2"))
            {
                spO2 = dictData["SpO2"];
            }
            else
            {
                spO2 = new OfficalDataThreshold();
                spO2.CreateDate = now;
                spO2.MeasureSubTypeId = dictType["SpO2"].Id;
            }
            spO2.Max = obj.SpO2_Max;
            spO2.Min = obj.SpO2_Min;
            spO2.ModifyDate = now;

            list.Add(spO2);
            OfficalDataThreshold pulse;
            if (dictData.ContainsKey("Pulse"))
            {
                pulse = dictData["Pulse"];
            }
            else
            {
                pulse = new OfficalDataThreshold();
                pulse.CreateDate = now;
                pulse.MeasureSubTypeId = dictType["Pulse"].Id;
            }
            pulse.Max = obj.Pulse_Max;
            pulse.Min = obj.Pulse_Min;
            pulse.ModifyDate = now;
            list.Add(pulse);

            OfficalDataThreshold temp;
            if (dictData.ContainsKey("Temp"))
            {
                temp = dictData["Temp"];
            }
            else
            {
                temp = new OfficalDataThreshold();
                temp.CreateDate = now;
                temp.MeasureSubTypeId = dictType["Temp"].Id;
            }
            temp.Max = obj.Temp_Max;
            temp.Min = obj.Temp_Min;
            temp.ModifyDate = now;
            list.Add(temp);

            OfficalDataThreshold ecg;
            if (dictData.ContainsKey("ECG"))
            {
                ecg = dictData["ECG"];
            }
            else
            {
                ecg = new OfficalDataThreshold();
                ecg.CreateDate = now;
                ecg.MeasureSubTypeId = dictType["ECG"].Id;
            }
            ecg.Max = obj.ECG_Max;
            ecg.Min = obj.ECG_Min;
            ecg.ModifyDate = now;
            list.Add(ecg);

            IOfficalDataThresholdService service = new OfficalDataThresholdService();
            service.InsertOrUpdate(list);
            getData();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void ListBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            IMeasureUserService muSer = new MeasureUserService();
            var testUser = muSer.Find(c => c.UserTypeId == (int)UserType.TestUser).FirstOrDefault();
            if (testUser != null)
            {
                PMWindow window = new UserForm(testUser);
                window.Title = "编辑用户体验帐号";
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                window.ShowDialog();
            }
            else
            {
                UserForm window = new UserForm();
                window.IsUserTest = true;
                window.Title = "新增用户体验帐号";
                window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                window.ShowDialog();
            }
        }
    }

}
